﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;

namespace BawangMemo.BackendModels
{
    public class Constants
    {
        public const int WORD_INFO_SIZE = 512;

        #region Directories

        private const string COURSE_DIRECTORY = "AppData/Course/";

        private const string BOOK_DIRECTORY = "AppData/Book/";

        private const string COURSE_LIST = "course.lst";

        private const string DB_PATH = "AppData/db.accdb";

        private const string CONNECTION_STRING_PREFIX = "Provider = Microsoft.ACE.OLEDB.12.0;Data Source = ";

        public static string CourseListFileName
        {
            get
            {
                if (!File.Exists(CourseDirectory + COURSE_LIST))
                {
                    CreateXML(CourseDirectory + COURSE_LIST);
                }
                return CourseDirectory + COURSE_LIST;
            }
        }

        private static void CreateXML(string path)
        {
            using (XmlWriter writer = XmlWriter.Create(path))
            {
                writer.WriteStartDocument();
                writer.WriteStartElement(COURSE_LIST_ROOT_ELEMENT);
                writer.WriteEndElement();
                writer.Flush();
                writer.Close();
            }
        }

        public static string CourseDirectory
        {
            get
            {
                if (!Directory.Exists(COURSE_DIRECTORY))
                {
                    Directory.CreateDirectory(COURSE_DIRECTORY);
                    Console.WriteLine("Created " + COURSE_DIRECTORY);
                }
                return COURSE_DIRECTORY;
            }
        }

        public static string ConnectionString
        {
            get
            {
                if (!File.Exists(DB_PATH))
                {
                    // TODO
                    // Create Database File
                }
                return CONNECTION_STRING_PREFIX + DB_PATH;
            }
        }

        public static string BookDirectory
        {
            get
            {
                if (!Directory.Exists(BOOK_DIRECTORY))
                {
                    Directory.CreateDirectory(BOOK_DIRECTORY);
                    Console.WriteLine("Created " + BOOK_DIRECTORY);
                }
                return BOOK_DIRECTORY;
            }
        }

        #endregion

        #region File Extensions

        public const string EXT_WORD_LIST = ".xml";

        public const string EXT_WORD_INFO = ".inf";

        public const string EXT_WORD_TAG = ".tag";

        public const string EXT_COURSE_INFO = ".crs";

        public const string COURSE_LIST_ROOT_ELEMENT = "courses";

        public const string COURSE_LIST_SUB_ELEMENT = "course";

        public const string WORD_LIST_ROOT_ELEMENT = "words";

        public const string WORD_LIST_SUB_ELEMENT = "word";

        public const string DATE_FORMAT = "yyyyMMdd";

        #endregion
    }
}
